#include "groupchat.h"
#include "ui_groupchat.h"
#include "interface_control.h"

#include <QTimer>
#include <QDateTime>

GroupChat::GroupChat(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GroupChat)
{
    qDebug()<<"进入聊天界面";
    ui->setupUi(this);
    this->setWindowTitle(frend.name);
    ui->MessageEdit->setReadOnly(true);
//    Interface_Control::getControlptr()->set_m_gro(this);


//    connect(ui->SendBtn, &QPushButton::clicked, this, &GroupChat::on_SendBtn_clicked);

}

void GroupChat::recvMessage(QString str)
{
    QString message = str;
    appendMessage(frend.name,str,Qt::AlignLeft, Qt::blue);
    return;
}


//void GroupChat::onReadyRead()
//{
//    QByteArray data = socket->readAll();
//    QString message = QString::fromUtf8(data);
//  appendMessage("Server: " + message, Qt::AlignLeft, Qt::blue);
//    //qDebug() << "收到数据：" << data;
//    // 假设服务器发送的消息以特殊标记区分，如"USERLIST:"标记用户列表
//       if (message.startsWith("USERLIST:"))
//       {
//           updateUserList(message);
//       }
//       else
//       {
//           appendMessage("Server", message, Qt::AlignLeft, Qt::blue);
//       }
//       //qDebug() << "收到数据：" << data;
//}





void GroupChat::on_SendBtn_clicked()
{
    //时间
    QDateTime currentTime = QDateTime::currentDateTime();
    QString timeString = currentTime.toString("hh:mm:ss");
   //文本
    QString message = ui->SendEdit->toPlainText();
    //封装
    QJsonObject js ;
    js.insert("cmd",2);
    js.insert("receiver",frend.friendloginid);
    js.insert("sender",Client_connet::Get_Connet()->me.ID);
    js.insert("message",message);
    js.insert("time",timeString);

    qDebug()<<"消息发送测试："<<js;
    //发送
    Client_connet::Get_Connet()->set_obj(js);
    Client_connet::Get_Connet()->SendServer();

    //本端显示
    if (!message.isEmpty())
    {
        timeString = currentTime.toString("hh:mm:ss");
        QString formattedMessage = "[" + timeString + "] " + message;

        appendMessage("You", message, Qt::AlignRight, Qt::red);
        ui->SendEdit->clear();
    }

}
void GroupChat::appendMessage(const QString &sender, const QString &message, Qt::Alignment alignment, const QColor &textColor)
{
    QTextCursor cursor = ui->MessageEdit->textCursor();
    cursor.movePosition(QTextCursor::End);
    ui->MessageEdit->setTextCursor(cursor);

    QTextBlockFormat blockFormat;
    blockFormat.setAlignment(alignment);
    QTextCharFormat charFormat;
    charFormat.setForeground(QBrush(textColor)); // 设置文本颜色
    cursor.mergeCharFormat(charFormat);
    cursor.insertBlock(blockFormat);
    // 获取当前时间
    // 插入发送者和时间
    cursor.insertText(sender);
    cursor.insertText(" (");
    cursor.insertText(QDateTime::currentDateTime().toString("hh:mm:ss"));
    cursor.insertText("): ");

    cursor.insertText(message);
    ui->MessageEdit->ensureCursorVisible(); // 确保光标可见，以便滚动到底部
}



// 用户列表
void GroupChat::updateUserList(const QString &message)
{
    QStringList userList = message.mid(QString("USERLIST:").length()).split(",");
    ui->UserListWidget->clear();
    ui->UserListWidget->addItems(userList);
}
